﻿//创建插件
CKEDITOR.plugins.add('dcattachment', {
    init: function(editor){
        //新建命令
        editor.addCommand('dcattachment',{
            exec: function(editor){
                daicuo.ajax.get(
                    //加载上传表单
                    daicuo.config.root+'attachment/editor/index',
                    //加载上传表单成功
                    function(data, status, xhr){
                        //展示上传表单
                        daicuo.bootstrap.dialogForm(data);
                        //加载上传组件
                        CKEDITOR.DCATTACHMENT(editor);
                    },
                    //加载上传表单失败
                    function(xhr, status, data){
                        daicuo.bootstrap.dialog(xhr+'<a class="text-purple text-decoration-none ml-1" href="https://www.daicuo.org/store/attachment" target="_blank">请先安装附件管理插件</a>');
                    }
                );
            }
        });
        //新建按钮
        editor.ui.addButton('dcattachment',{
            label: '附件',
            command: 'dcattachment',
            icon: this.path + 'attachment.svg'
        });
    }
});
//扩展
CKEDITOR.DCATTACHMENT= function(editor){
    var attachment = {
        //附件列表页
        editor: {
            //监听插入链接
            insert: function(){
                $(document).on("click",'[data-toggle="attachment-insert"]',function(){
                    if($(this).parent().data('image')){
                        editor.insertHtml('<img class="img-fluid" alt="'+$(this).parent().data('title')+'" src="'+$(this).parent().data('image')+'" /><br />');
                    }else{
                        if($(this).parent().data('score')){
                            var fileName = $(this).parent().data('title')+'（需要'+$(this).parent().data('score')+'个积分）';
                        }else{
                            var fileName = $(this).parent().data('title');
                        }
                        editor.insertHtml('<a class="dc-file" href="'+$(this).attr('href')+'">'+fileName+'</a><br />');
                    }
                    return false;
                });
            },
            //监听删除链接
            delete: function(){
                $(document).on("click",'[data-toggle="attachment-delete"]',function(){
                    daicuo.ajax.get($(this).attr('href'),function(json,status,xhr){
                        attachment.editor.get($('[data-toggle="attachment-form"]').attr('action'));
                    },function(json,status,html){
                        daicuo.bootstrap.dialog(status);
                    });
                    return false;
                });
            },
            //监听修改链接
            edit: function(){
                $(document).on("click",'[data-toggle="attachment-edit"]',function(){
                    daicuo.ajax.get($(this).attr('href'),function(html,status,xhr){
                        //动态加载MODAL
                        attachment.editor.modalShow('modal-edit', html);
                        //修改页表单事件
                        attachment.edit.onSubmit();
                        //修改页上传事件
                        attachment.edit.onUpload();
                    },function(json,status,html){
                        daicuo.bootstrap.dialog(status);
                    });
                    return false;
                });
            },
            //监听下一页按钮
            next: function(){
                $(document).on("click",'[data-toggle="attachment-start"]',function(){
                    attachment.editor.get(attachment.editor.urlNext());
                });
            },
            //监听搜索表单
            search: function(){
                $(document).on('submit','[data-toggle="attachment-form"]', function() {
                    daicuo.ajax.post($(this).attr('action'), $(this).serialize(), function(html, status, xhr) {
                        daicuo.bootstrap.dialogForm(html);
                    });
                    return false;
                });
            },
            //请求附件列表
            get: function(url){
                daicuo.ajax.get(url,function(html,status,xhr){
                    daicuo.bootstrap.dialogForm(html);
                },function(xhr,status,html){
                    daicuo.bootstrap.dialog(status);
                });
            },
            //显示MODAL(modal-edit)
            modalShow: function(element,html){
                $('#'+element).remove();
                $(".dc-modal").after('<div class="modal" id="'+element+'"><div class="modal-dialog modal-sm modal-dialog-centered"><div class="modal-content"><div class="modal-body">' + html + '</div></div></div></div>');
                $('#'+element).modal('show');
            },
            //隐藏MODAL(#modal-edit)
            modalHide: function(element){
                $('#'+element).modal('hide');
            },
            //当前页URL
            urlReferer: function(){
                return $('[data-toggle="attachment-form"]').attr('action')+'?searchText='+$('[data-toggle="attachment-search"]').val()+'&pageStart='+$('[data-toggle="attachment-start"]').data('referer');
            },
            //下一页URL
            urlNext: function(){
                return $('[data-toggle="attachment-form"]').attr('action')+'?searchText='+$('[data-toggle="attachment-search"]').val()+'&pageStart='+$('[data-toggle="attachment-start"]').data('start');
            }
        },
        //附件修改页
        edit: {
            onSubmit: function(){
                $(document).off('submit','[data-toggle="attachment-edit-form"]').on('submit','[data-toggle="attachment-edit-form"]', function() {
                    daicuo.ajax.post($(this).attr('action'), $(this).serialize(), function(json, status, xhr) {
                        attachment.editor.modalHide('modal-edit');
                        attachment.editor.get(attachment.editor.urlReferer());
                    });
                    return false;
                });
            },
            onUpload: function(){
                daicuo.upload.init({
                    element: '#attachment-edit-upload',
                    container: 'editorEditUpload',
                    multiple: false,
                    params: {
                        id: $('#attachment-edit-id').val(),
                        score: $('#attachment-edit-score').val()
                    },
                    onInit: function(up){
                        $(document).on('change', '#attachment-edit-score', function(){
                            up.setOption("params", {
                               score : $(this).val()
                            })
                        });
                    },
                    onError: function(up, data){
                        attachment.editor.modalShow(data.msg);
                    },
                    onSuccess: function(up, file){
                        attachment.editor.modalHide('modal-edit');
                        attachment.editor.get(attachment.editor.urlReferer());
                    }
                });
            }
        }
    };
    //插入
    attachment.editor.insert();
    //删除
    attachment.editor.delete();
    //修改
    attachment.editor.edit();
    //下一页
    attachment.editor.next();
    //搜索
    attachment.editor.search();
};